import 'package:fish_redux/fish_redux.dart';
import 'package:flutter/material.dart' hide Action;
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:www.znpigai.teacher/pay_list_page/page.dart';
import 'package:www.znpigai.teacher/pay_teacher_list_page/page.dart';
import 'package:www.znpigai.teacher/practice_edit_page/page.dart';
import 'package:www.znpigai.teacher/practice_list_page/page.dart';
import 'package:www.znpigai.teacher/practice_voice_page/page.dart';
import 'package:www.znpigai.teacher/profile_page/page.dart';
import 'package:www.znpigai.teacher/social_login_page/page.dart';
import 'package:www.znpigai.teacher/student_list_page/page.dart';
import 'package:www.znpigai.teacher/subject_edit_page/page.dart';
import 'package:www.znpigai.teacher/subject_page/page.dart';
import 'package:www.znpigai.teacher/text_edit_page/page.dart';
import 'package:www.znpigai.teacher/utils/nav.dart';
import 'package:www.znpigai.teacher/web_page/page.dart';
import 'package:www.znpigai.teacher/web_share_page/page.dart';

import 'about_page/page.dart';
import 'answer_edit_page/page.dart';
import 'answer_mark_page/page.dart';
import 'avatar_page/page.dart';
import 'boot_guide_page/page.dart';
import 'camera_guide_page/page.dart';
import 'camera_page/page.dart';
import 'class_list_page/page.dart';
import 'demand_page/page.dart';
import 'destroy_page/page.dart';
import 'global_store/state.dart';
import 'global_store/store.dart';
import 'home_page/page.dart';
import 'home_page/subject_tab_component/page.dart';
import 'homework_detail_page/page.dart';
import 'homework_post_page/page.dart';
import 'login_page/page.dart';
import 'login_page/state.dart';
import 'message_list_page/page.dart';
import 'todo_edit_page/page.dart';
import 'todo_list_page/page.dart';

////create global page helper
//Page<T, dynamic> pageConfiguration<T extends GlobalBaseState<T>>(
//    Page<T, dynamic> page) {
//  return page
//
//    ///connect with app-store
//    ..connectExtraStore(GlobalStore.store, (T pagestate, GlobalState appState) {
//      return pagestate.themeColor == appState.themeColor
//          ? pagestate
//          : ((pagestate.clone())..themeColor = appState.themeColor);
//    })
//
//    ///updateMiddleware
//    ..updateMiddleware(
//      view: (List<ViewMiddleware<T>> viewMiddleware) {
//        viewMiddleware.add(safetyView<T>());
//      },
//      adapter: (List<AdapterMiddleware<T>> adapterMiddleware) {
//        adapterMiddleware.add(safetyAdapter<T>());
//      },
//    );
//}

Widget createApp() {
  final AbstractRoutes routes = PageRoutes(
    pages: <String, Page<Object, dynamic>>{
      Nav.HOME: HomePage(),
      Nav.TODO_LIST: ToDoListPage(),
      Nav.TODO_EDIT: TodoEditPage(),
      Nav.LOGIN: LoginPage(),
      Nav.WEB_PAGE: WebPage(),
      Nav.WEB_SHARE_PAGE: WebSharePage(),
      Nav.SOCIAL_LOGIN: SocialLoginPage(),
      Nav.AVATAR_LIST: AvatarPage(),
      Nav.PROFILE: ProfilePage(),
      Nav.PAY: GlobalState.isTeacher ? PayTeacherListPage() : PayListPage(),
      Nav.ABOUT: AboutPage(),
      Nav.MESSAGE_LIST: MessageListPage(),
      Nav.ANSWER_EDIT: AnswerEditPage(),
      Nav.ANSWER_MARK: AnswerMarkPage(),
      Nav.CLASS_LIST: ClassListPage(),
      Nav.CLASS_EDIT: WebPage(),
      Nav.CLASS_GUIDE: WebPage(),
      Nav.DEMAND: DemandPage(),
      Nav.HOMEWORK_DETAIL: HomeworkDetailPage(),
      Nav.HOMEWORK_POST: HomeworkPostPage(),
      Nav.PRACTICE_CAMERA: CameraPage(),
      Nav.PRACTICE_EDIT: PracticeEditPage(),
      Nav.PRACTICE_GUIDE: CameraGuidePage(),
      Nav.PRACTICE_LIST: PracticeListPage(),
      Nav.PRACTICE_VOICE: PracticeVoicePage(),
      Nav.SUBJECT_LIST: SubjectTabPage(),
      Nav.SUBJECT_DETAIL: SubjectPage(),
      Nav.SUBJECT_EDIT: SubjectEditPage(),
      Nav.STUDENT_LIST: StudentListPage(),
      Nav.TEXT_EDIT: TextEditPage(),
      Nav.BOOT_GUIDE: BootGuidePage(),
      Nav.DESTROY: DestroyPage(),
      // --------------------------------------
      Nav.STUDENT_EDIT: WebPage(),
      Nav.STUDENT_DETAIL: WebPage(),
      Nav.SUBJECT_MINE: WebPage(),
      Nav.SUBJECT_POST: WebPage(),
      Nav.HOMEWORK_POST_SHOW: WebPage(),
      Nav.HOMEWORK_POST_EDIT: WebPage(),
      Nav.HOMEWORK_DETAIL_CONTAINER: WebPage(),
      Nav.ANSWER_DETAIL: WebPage(),
      Nav.ANSWER_AI_INPUT: WebPage(),
      Nav.ANSWER_AI_MARK: WebPage(),
      Nav.CHANGE_PASSWORD: WebPage(),
      Nav.HOMEWORK_WEB_VIEW: WebPage(),
      Nav.HOMEWORK_LIST: WebPage(),
      Nav.ANSWER_LIST: WebPage(),
      Nav.PROFILE_EDIT: WebPage(),
      Nav.PRACTICE_HOME: WebPage(),
      Nav.PRACTICE_DETAIL: WebPage(),
      Nav.PRACTICE_PHOTO: WebPage(),
      Nav.SUBJECT_CONTAINER: WebPage(),
    },
    visitor: (String path, Page<Object, dynamic> page) {
      /// XXX
      if (page.isTypeof<GlobalBaseState>()) {
        page.connectExtraStore<GlobalState>(GlobalStore.store,
            (Object pagestate, GlobalState appState) {
          final GlobalBaseState p = pagestate;
          if (p.themeColor == appState.themeColor &&
              p.userInfo == appState.userInfo) {
            return pagestate;
          } else {
            if (pagestate is Cloneable) {
              final Object copy = pagestate.clone();
              final GlobalBaseState newState = copy;
              newState.themeColor = appState.themeColor;
              newState.userInfo = appState.userInfo;
              return newState;
            }
          }
          return null; // fix no return
        });
      }

      page.enhancer.append(
        viewMiddleware: <ViewMiddleware<dynamic>>[safetyView<dynamic>()],
        adapterMiddleware: <AdapterMiddleware<dynamic>>[
          safetyAdapter<dynamic>()
        ],
        effectMiddleware: [],
        middleware: <Middleware<dynamic>>[logMiddleware<dynamic>()],
      );

      // }
      ///updateMiddleware
      /// TODO
      // ..updateMiddleware(
      //   view: (List<ViewMiddleware<T>> viewMiddleware) {
      //     viewMiddleware.add(safetyView<T>());
      //   },
      //   adapter: (List<AdapterMiddleware<T>> adapterMiddleware) {
      //     adapterMiddleware.add(safetyAdapter<T>());
      //   },
      // )
    },
  );

  return MaterialApp(
    localizationsDelegates: [
      // ... app-specific localization delegate[s] here
      GlobalMaterialLocalizations.delegate,
      GlobalWidgetsLocalizations.delegate,
      GlobalCupertinoLocalizations.delegate,
    ],
    supportedLocales: [
      const Locale('en'), // English
      const Locale.fromSubtags(
          languageCode: 'zh'), // Chinese *See Advanced Locales below*
    ],
    title: GlobalState.isTeacher ? '作文批改教师版' : '作文批改学生版',
    debugShowCheckedModeBanner: false,
    theme: ThemeData(
      // platform: TargetPlatform.iOS,
      brightness: Brightness.light,
      primaryColor: Colors.white,
      accentColor: Colors.black54,
      pageTransitionsTheme: PageTransitionsTheme(builders: {
        TargetPlatform.android: CupertinoPageTransitionsBuilder(),
      }),
    ),
    home: routes.buildPage(GlobalState.getHomePage(), null),
    onGenerateRoute: (RouteSettings settings) {
      return MaterialPageRoute<Object>(builder: (BuildContext context) {
        return routes.buildPage(settings.name, settings.arguments);
      });
    },
  );
}
